package scau.erps.view.base;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import scau.erps.app.Erps;
import scau.erps.entity.Line;
import scau.erps.service.LineService;
import scau.erps.service.impl.LineServiceImpl;

/**
 * 出售生产线界面
 * @author outsider
 * 
 * 
 */
public class SellLineDialog extends JDialog {	
	private static final long serialVersionUID = 213471827269396255L;
	private final JPanel contentPanel = new JPanel();
    private JComboBox<String> sellLineComboBox;
    private LineService lineService=Erps.getIocInstance().get(LineServiceImpl.class);
    private int team_Id=Erps.getCurrentTeam().getId();
    private DecisionPanel superPanel;
    private boolean show=true;
	/**
	 * Create the dialog.
	 */
	public SellLineDialog(DecisionPanel superPanel) {
		this.superPanel=superPanel;
		this.setModal(true);
		this.setTitle("变卖生产线");
		this.setSize( 300, 225);
		this.setLocationRelativeTo(null);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		JLabel label = new JLabel("请选择可变卖的生产线");
		label.setBounds(30, 10, 200, 35);
		contentPanel.add(label);		
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton sellButton = new JButton("确定变卖");
				sellButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						sellButtonAction();
						
					}
				});
				buttonPane.add(sellButton);
				getRootPane().setDefaultButton(sellButton);
			}
			{
				JButton exitButton = new JButton("退出");
				exitButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
				buttonPane.add(exitButton);
			}
		}
		getSellLine();
		contentPanel.add(sellLineComboBox); 
		if (show) setVisible(true);
	}
	private void getSellLine(){                              //获取可出售生产线添加入Combobox中
		sellLineComboBox = new JComboBox<String>();
		sellLineComboBox.setBounds(30, 50, 200, 35);
		List<Line> list=lineService.getLineByTeamId(team_Id);
		for (Line line:list){
			int lineNo=line.getLineNo();
			String status=line.getStatus();
			String type=line.getType();
			int remain=0;
			if (type.equals("手工线")) remain=1;
			if (type.equals("半自动")) remain=2;
			if (type.equals("全自动")) remain=3;
			if (type.equals("柔性线")) remain=4;
			if (status.equals("全新")||status.equals("空闲")){
                String message=lineNo+"号线"+" "+type+" 残值"+remain+"M";
                sellLineComboBox.addItem(message);
			}	  
		}
		if (sellLineComboBox.getItemCount()==0){
			JOptionPane.showMessageDialog(this, "当前无出售生产线","提示",JOptionPane.INFORMATION_MESSAGE);
			show=false;
		}
	}
	private void sellButtonAction(){
		int lineNo=Integer.parseInt(sellLineComboBox.getSelectedItem().toString().substring(0, 1));
		int opinion=JOptionPane.showConfirmDialog(this, "是否确认出售"+lineNo+"号生产线", "确认出售", JOptionPane.YES_NO_OPTION);
		if (opinion==JOptionPane.YES_OPTION){
			int remain=lineService.sellLine(team_Id, lineNo);
			sellLineComboBox.removeItemAt(sellLineComboBox.getSelectedIndex());
			JOptionPane.showMessageDialog(null, "出售成功，获得"+remain+"M残值放入现金区域"+"\n"+"扣除残值后如有剩余净值需提取本年度折旧；"+"\n"+"提取折旧后如有剩余净值则为损失，放入其他区域", "提示",JOptionPane.INFORMATION_MESSAGE);
			superPanel.getDashBoardPanel().setFactoryData();
			superPanel.getDashBoardPanel().setDashBoardData(17);
			if (sellLineComboBox.getItemCount()==0){
				dispose();
			}
		}
	}
}
